﻿using System;
using System.Data.Common;
using System.IO;
using System.Reflection;

namespace YJKDataBase
{
	public class SqliteDatabase : RelationDatabase, IDisposable
	{
		public static string SqlitePath
		{
			get
			{
				return Path.Combine(SqliteDatabase.GetAppRootPath(), "bin\\" + SqliteDatabase.SqliteName);
			}
		}

		public static string GetAppRootPath()
		{
			string location = Assembly.GetExecutingAssembly().Location;
			int length = location.LastIndexOf("\\");
			string text = location.Substring(0, length);
			length = text.LastIndexOf("\\");
			return text.Substring(0, length);
		}

		public override string ConnStr
		{
			get
			{
				return "Data Source=" + this.mName + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			}
		}

		public SqliteDatabase(string fileName)
		{
			this.mName = fileName;
			this.Sqlite = Assembly.LoadFrom(SqliteDatabase.SqlitePath);
			object obj = this.Sqlite.CreateInstance(SqliteDatabase.SQLiteConnection, true, BindingFlags.CreateInstance, null, new object[]
			{
				this.ConnStr
			}, null, new object[0]);
			obj.GetType().GetMethod("Open").Invoke(obj, new object[0]);
			this.dbCon = (obj as DbConnection);
		}

		public override ISubjectTable SubjectTable
		{
			get
			{
				return new SqliteSubjectTable(this);
			}
		}

		public override ICategoryTable CategoryTable
		{
			get
			{
				return new SqliteCategoryTable(this);
			}
		}

		public override IObjectTable ObjectTable
		{
			get
			{
				return new SqliteObjectTable(this);
			}
		}

		public override IObjectFileTable ObjectFileTable
		{
			get
			{
				return new SqliteObjectFileTable(this);
			}
		}

		public SqliteDatabase CopyForUpdate()
		{
			return null;
		}

		public void Dispose()
		{
			this.dbCon.Close();
			this.dbCon.Dispose();
		}

		public static string SqliteName = "System.Data.SQLite.dll";

		public static string SQLiteConnection = "System.Data.SQLite.SQLiteConnection";

		public static string SQLiteParameter = "System.Data.SQLite.SQLiteParameter";

		public Assembly Sqlite;
	}
}
